package com.example.note2;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class NoteDatabase extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "notes";
    public static final String CONTENT = "content";
    public static final String ID = "id";
    public static final String TIME = "time";
    public static final String MODE = "mode";
    public static final String USER_ID = "user_id";


    public static final String USER_TABLE_NAME = "users"; // 用户表
    public static final String USER_NAME = "username"; // 用户名
    public static final String USER_PASSWORD = "password"; // 用户密码


    public NoteDatabase(Context context) {
        super(context,"notes", null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + CONTENT + " TEXT NOT NULL, "
                + TIME + " TEXT NOT NULL, "
                + MODE + " INTEGER DEFAULT 1, "
                + USER_ID + " TEXT NOT NULL)"); // 用户ID
        db.execSQL("CREATE TABLE " + USER_TABLE_NAME + "(" +
                USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                USER_NAME + " TEXT NOT NULL UNIQUE, " +
                USER_PASSWORD + " TEXT NOT NULL)");
    }

    //版本更新时调用
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//        for (int i = oldVersion; i < newVersion; i++) {
//            switch (i) {
//                case 1:
//                    // upgrade logic from version 1 to 2
//                    break;
//                case 2:
//                    // upgrade logic from version 2 to 3
//                    updateMode(sqLiteDatabase);
//                default:
//                    break;
//            }
//        }

    }
    public boolean checkTableExists(SQLiteDatabase db, String tableName) {
        Cursor cursor = db.rawQuery("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", new String[] {"table", tableName});
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count > 0;
    }
    private void updateMode(SQLiteDatabase sqLiteDatabase) {
    }
}
